package cn.eeepay.boss.system;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(5)
public class DaoAspet {

    private final static Logger log = LoggerFactory.getLogger(DaoAspet.class);

    /**
     * 切入点
     */
    @Pointcut("@annotation(cn.eeepay.boss.system.DataSource)")
    public void read(){}

    @Before("read() && @annotation(dataSource)")
    public void getDataSource(JoinPoint point, DataSource dataSource){
        DynamicDataSourceHolder.putDataSource(dataSource.value());
        //调试代码，可注释
        String className = point.getTarget().getClass().getSimpleName();
        String methodName = point.getSignature().getName();
        log.info("dataSource切换到：{} 开始执行: {}.{}() 方法", dataSource.value(), className, methodName);
    }

    /**
     * 清楚数据源一方面为了避免内存泄漏，更重要的是避免对后续在本线程上执行的操作产生影响
     */
    @After("read()")
    public void after() {
        DynamicDataSourceHolder.clearDbType();
    }

//    @After("read()")
//    public void afterDynamicSource(){
//        DynamicDataSourceHolder.clearDbType();
//    }

}
